<?php

    require_once Core_IO_Path::combine(SERUM_MODULE_PATH, 'Base', 'controllers', 'admin', 'IndexController.php');

    /**
     * @package      Modules
     * @subpackage   Languages_Controllers_Admin
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
     class Languages_Controllers_Admin_Index extends Base_Controllers_Admin_Index
     {

    ////////////////////////////////////////////////////////////////////////////
    //                                Variables                               //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Model class
         *
         * @var string
         */
        protected $_model = 'DbLanguage';

		/**
         * Parent class
         *
         * @var string
         */
		protected $_parent = 'DbCountry';


    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Loads the edition form
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function edit()
        {
            $rep    = $this->_getResponse('json');
            $fields = $this->_prepareEditionVariables($rep, __FUNCTION__, $this->_model);

            $displays = Doctrine_Query::create()
                            ->from('DbCountryLanguage cl')
                            ->where('cl.country_id = ?', $this->_intParam('parentRoot'))
                            ->addWhere('cl.language_id = ?', $this->_intParam('elementId'))
                            ->fetchOne();

            $fields['fields']['default']['displayInFront'] = array(
                                                                    'type'      => 'boolean',
                                                                    'value'     => $displays->displayInFront,
                                                                    'fieldname' => 'displayInFront'
                                                                  );

            $fields['fields']['default']['displayInBack'] = array(
                                                                    'type'      => 'boolean',
                                                                    'value'     => $displays->displayInBack,
                                                                    'fieldname' => 'displayInBack'
                                                                  );

            $rep->setResponseContent($fields);

            return $rep;
        }

        /**
         * Saves the modifications
         *
         * @throws      Core_Exception
         * @return      Core_Controller_Response_Abstract
         */
        public function save()
        {
            $rep = parent::save();

            $displays = Doctrine_Query::create()
                            ->from('DbCountryLanguage cl')
                            ->where('cl.country_id = ?', $this->_intParam('parentRoot'))
                            ->addWhere('cl.language_id = ?', $this->_intParam('id'))
                            ->fetchOne();

            if ($displays && $this->_getParam('displayInFront') !== null && $this->_getParam('displayInBack') !== null) {
                $displays->displayInFront   = $this->_boolParam('displayInFront');
                $displays->displayInBack    = $this->_boolParam('displayInBack');
                $displays->save();
            }

            return $rep;
        }
    }
